const {
	reqGet,
	reqPost,
} = require('@utils/request')
const {
	$U_UserInfo,
} = require('@utils/storage')

module.exports = {
	/**
	 * 添加请求参数
	 */
	addParameter() {
		endEditRow(this)
		let {
			store,
			parameterList: paramsComp,
		} = this
		if (!store.isLock)
			return false
		let newId = randomId()
		let current = store.currentParameter
		paramsComp.treegrid('append', {
			data: [{
				_treeId_: newId,
				name: '',
				type: 'string',
				content: '',
				remark: '',
				mandatory: 'O',
			}]
		});
		let editRow = {
			_treeId_: newId,
			name: '',
			type: 'string',
			content: '',
			remark: '',
			mandatory: 'O',
		}
		current.editRow = editRow
		paramsComp.treegrid('beginEdit', newId)
	},

	/**
	 * 导入请求JSON格式数据
	 * @return {[type]} [description]
	 */
	importJSON() {
		endEditRow(this)
		let {
			store,
			dailogComp,
			jsonText,
		} = this
		if (!store.isLock)
			return false
		let jsonDialog = dailogComp.jsonDialog
		jsonText.textbox('setValue', '')
		jsonDialog.dialog('open')
	},

	parameterChange(newValue, oldValue, field) {
		if (typeof newValue == 'undefined')
			return false
		let {
			store,
		} = this
		let current = store.currentParameter
		current.editRow[field] = newValue
	},

	// parameterNameChage(newValue, oldValue, flag) {
	// 	if (typeof newValue == 'undefined')
	// 		return false
	// 	let {
	// 		store,
	// 	} = this;
	// 	let current = store[`${flag}Current`]
	// 	current.editRow.name = newValue
	// },

	// parameterContentChage(newValue, oldValue, flag) {
	// 	if (typeof newValue == 'undefined')
	// 		return false
	// 	let {
	// 		store,
	// 	} = this;
	// 	let current = store[`${flag}Current`]
	// 	current.editRow.content = newValue
	// },

	// parameterTypeChage(newValue, oldValue, flag) {
	// 	if (typeof newValue == 'undefined')
	// 		return false
	// 	let {
	// 		store,
	// 	} = this;
	// 	let current = store[`${flag}Current`]
	// 	current.editRow.type = newValue

	// },

	// parameterRemarkChage(newValue, oldValue, flag) {
	// 	if (typeof newValue == 'undefined')
	// 		return false
	// 	let {
	// 		store,
	// 	} = this;
	// 	let current = store[`${flag}Current`]
	// 	current.editRow.remark = newValue
	// },

	addParameterChildren(e) {
		let {
			store,
			parameterList: paramsComp,
		} = this
		let current = store.currentParameter
		let parentRow = current.parentRow
		let newId = randomId()
		paramsComp.treegrid('append', {
			parent: parentRow._treeId_,
			data: [{
				_treeId_: newId,
				name: '',
				type: 'string',
				content: '',
				remark: '',
				mandatory: 'O',
			}]
		});
		let editRow = {
			_treeId_: newId,
			name: '',
			type: 'string',
			content: '',
			remark: '',
			mandatory: 'O',
		}
		current.editRow = editRow
		paramsComp.treegrid('beginEdit', newId)
	},

	deleteParameter(e) {
		let {
			store,
			parameterList: paramsComp,
		} = this
		let current = store.currentParameter
		let parentRow = current.parentRow
		paramsComp.treegrid('remove', parentRow._treeId_)
	},

	/**
	 * 右键显示菜单
	 * 1.添加子属性(type = 'object')
	 * 2.删除属性
	 * @param  {[type]} e   [description]
	 * @param  {[type]} row [description]
	 * @return {[type]}     [description]
	 */
	showParameterMenu(e, row) {
		e.preventDefault()
		endEditRow(this)
		let {
			store,
			parameterList: paramsComp,
			components,
		} = this
		if (!store.isLock)
			return false
		let paramsMenu = components.parameterMenu;
		let current = store.currentParameter
		let hideItem = paramsMenu.menu('findItem', '添加属性')
		let opts = row.type != 'object' && row.type != 'array[object]' ? 'hideItem' : 'showItem'
		paramsMenu.menu(opts, hideItem.target)
		paramsMenu.menu('show', {
			left: e.pageX,
			top: e.pageY,
		})
		current.parentRow = row
		paramsComp.treegrid('select', row._treeId_)
	},

	onDblClickRow(row) {
		endEditRow(this)
		let {
			store,
			parameterList: paramsComp,
		} = this
		if (!store.isLock)
			return false
		let current = store.currentParameter
		current.editRow = row
		paramsComp.treegrid('beginEdit', row._treeId_);
	},

	onClickRow(row) {
		endEditRow(this)
	},

	saveJSONMethod() {
		let {
			store,
			jsonText,
			dailogComp,
			parameterList: paramsComp,
		} = this
		let jsonDialog = dailogComp.jsonDialog
		if (paramsComp) {
			let JSONData = null
			let value = jsonText.textbox('getValue')
			try {
				JSONData = JSON.parse(value)
			} catch (e) {
				try {
					JSONData = new Function(`return ${value};`)()
				} catch (e) {
					jsonData = null
				}
				if (!JSONData || typeof JSONData != 'object') {
					alert('JSON语法错误')
					return false
				}
			}
			addParamsComp.call(this, JSONData)
		}
		jsonDialog.dialog('close')
	},

}

/**
 * 结束编辑行
 * @param  {[type]} apiComp [description]
 * @return {[type]}         [description]
 */
function endEditRow(templateComp, flag) {
	let {
		store,
		parameterList: paramsComp,
		components,
	} = templateComp
	let paramsMenu = components.parameterMenu;
	let current = store.currentParameter
	if (current.editRow && current.editRow._treeId_)
		paramsComp.treegrid('endEdit', current.editRow._treeId_)
}

/**
 * 添加JSON格式数据
 * @param {[type]} JSONData [description]
 */
function addParamsComp(JSONData) {
	console.log('addParamsComp  JSONData', JSONData)
	let {
		store,
		parameterList: paramsComp,
	} = this
	let treeData = null
	if (Array.isArray(JSONData) && JSONData[0]._treeId_) {
		treeData = JSONData
	} else {
		let orgTreeData = paramsComp.treegrid('getData')
		JSONData = JSONData[0] || JSONData
		treeData = parseJSONParameter(JSONData)
		treeData = treeData.concat(orgTreeData)
	}
	paramsComp.treegrid('loadData', treeData)
}

/**
 * 解析导入JSON结构数据
 * @param  {[type]} JSONData [description]
 * @return {[type]}          [description]
 */
function parseJSONParameter(JSONData) {
	let paramArr = Object.keys(JSONData)
	let dataArr = []
	for (let i = 0; i < paramArr.length; i++) {
		let key = paramArr[i]
		let type = $._ulType(JSONData[key])
		let data = {
			_treeId_: randomId(),
			name: key,
			type,
			content: '',
			remark: '',
			mandatory: 'O',
		}
		if (Array.isArray(JSONData[key]) && JSONData[key].length > 0) {
			let arrVal = JSONData[key][0]
			data.type = `${type}[${$._ulType(arrVal)}]`
			if (typeof arrVal == 'object') {
				data.children = parseJSONParameter(arrVal)
				data.state = "open"
			}
		} else if (JSONData[key] && typeof JSONData[key] == 'object') {
			data.children = parseJSONParameter(JSONData[key])
			data.state = "open"
		}
		dataArr.push(data)
	}
	return dataArr
}

/**
 * 随机ID
 * @return {[type]} [description]
 */
function randomId() {
	return `${Math.random()}`.replace(/^0\./, '')
}